﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyGunner2 : EnemyBaseFixed
{
    public SpriteSequence standupSpriteSequence;

    public Sprite dieSprite;

    public SeqAction hideAction = new() { keepingTime = 2f };
    //public new SeqAction fireAction = new() { beginWaitTime = 0.1f, intervalTime = 0f, keepingTime = 2f }; 
    private SpriteRenderer sr;
    private Collider2D collider2d;

    private void Awake()
    {
        Init();
        needTurnAround = false;
        sr = GetComponent<SpriteRenderer>();
        collider2d = GetComponent<Collider2D>();
        collider2d.enabled = false;
        boomPrefab=Constant.Prefabs.EnemyBoom2.Load();

    }
    private void Start()
    {
        Stop();
    }

    public override Vector2 GetBodyPartPosition(Person.BodyPart bodyPart)
    {
        Vector2 ret;
        switch (bodyPart)
        {
            case Person.BodyPart.Head:
                ret = transform.position + (Vector3.up * 0.875f) + (Vector3.left * 0.25f * transform.localScale.x);
                break;
            case Person.BodyPart.Foot:
                ret = transform.position;
                break;
            default:
                ret = base.GetBodyPartPosition(bodyPart);
                break;
        }
        return ret;
    }

    private void FixedUpdate()
    {
        if (running)
        {
            base.CommonFixedUpdate();
        }
    }

    protected void UpdateStateNone(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = EnumState.Hidden;
    }
    protected void UpdateStateHidden(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = inState;
        if (stateChanged)
        {
            hideAction.ReStart();
        }
        collider2d.enabled = false;
        sr.sprite = standupSpriteSequence.configSequence[0].sprite;
        hideAction.Update((SeqAction seq) => { });
        if (hideAction.done)
        {
            outState = EnumState.StandingUp;
        }



    }
    protected void UpdateStateStandingUp(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = inState;
        if (stateChanged)
        {
            standupSpriteSequence.Restart();
        }
        collider2d.enabled = false;
        if (!standupSpriteSequence.Update(gameObject, transform.localScale))
        {
            outState = EnumState.StandUp;
        }

    }


    protected void UpdateStateStandUp(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = inState;

        if (stateChanged)
        {
            fireAction.ReStart();
        }
        collider2d.enabled = true;
        fireAction.Update((SeqAction seq) =>
        {
            if (seq.IsFirstAction())
            {
                seq.PropertyInteger["fired"] = 0;
            }
            if (seq.PropertyInteger["fired"] == 0)
            {
                Fire();
                seq.PropertyInteger["fired"] = 1;
            }
        });
        if (fireAction.done)
        {
            outState = EnumState.Hidden;
        }
    }

    protected override void UpdateStateDie(bool stateChanged, EnumState inState, out EnumState outState)
    {
        outState = inState;
        if (dieSprite != null)
        {
            sr.sprite = dieSprite;
        }

        base.UpdateStateDie(stateChanged, inState, out outState);
        dieAction.Update((SeqAction seq) =>
        {
            Vector3 diePosition = transform.position;
            if (!seq.PropertyVector.TryGetValue("diePosition", out diePosition))
            {
                diePosition = transform.position;
                seq.PropertyVector.Add("diePosition", diePosition);
            }
            int i = seq.seqNo;
            if (i < dieMovePixel.Length)
            {
                Vector2 pixel = dieMovePixel[i];
                float x = pixel.x / 16;
                float y = pixel.y / 16;
                transform.position = diePosition + new Vector3(lookAt.x * x, y, 0);
            }
            else
            {
                if (boomPrefab != null)
                {
                    Instantiate(boomPrefab, transform.position, Quaternion.identity);
                }
                Constant.Sounds.EnemyDeath1.PlayOneShot();
                Destroy(gameObject);
            }
        });
    }

    private void Fire()
    {
        Vector2 position = transform.position;
        Vector2 offset = new(0.9375f * transform.localScale.x, 0.75f);
        position += offset;
        if (bulletPrefab != null)
        {
            GameObject objBullet = Instantiate(bulletPrefab, position, Quaternion.identity);
            if (objBullet.TryGetComponent<ABullet>(out var aBullet))
            {
                aBullet.SetBulletType(ABullet.BulletType.C);
                aBullet.SetSource(gameObject);
                aBullet.Launch(Vector2.right * transform.localScale.x);
            }
        }
    }

}

